
const { controller_save,
    controller_find,
    controller_get,
    controller_delete, } = require("../../controller/order/ORDER.CONTROLLER");
const { serverError } = require("../../utils");
const { emptyCheck } = require("../../utils/checkEmpty");

/**
 * @description 订单路由
 * @author 韩
 */
const router = require("koa-router")()
router.prefix("/order");

/**@description 提交订单下单 */
router.post("/create", async (ctx, next) => {
    try {
        const {
            products = undefined,
            buyerUserId = undefined,
            buyerAddress = undefined,
        } = ctx.request.body
        const check = emptyCheck({ products, buyerUserId, buyerAddress })
        if (check.code !== 0) {
            ctx.body = check
        } else {
            const res = await controller_save({
                products: JSON.stringify(products), buyerUserId, buyerAddress: JSON.stringify(buyerAddress),
            })
            ctx.body = res
        }
    } catch (error) {
        console.log(error)
        ctx.body = serverError
    }
})




// /**@description 根据id获取分类详情 */
// router.get("/get", async (ctx, next) => {
//     try {
//         const id = ctx.query.id || undefined;
//         const res = await brand_controller_get({ id });
//         console.log("查询：\n", id)
//         ctx.body = res;
//     } catch (error) {
//         ctx.body = serverError
//     }
// })

// /**@description 根据id删除分类 */
// router.delete("/delete", async (ctx, next) => {
//     try {
//         const id = ctx.query.id || undefined;
//         const res = await brand_controller_delete({ ids: [id] });
//         ctx.body = res;
//     } catch (error) {
//         ctx.body = serverError
//     }
// })


// 查询列表
router.post("/list", async (ctx, next) => {
    try {
        const { current, pageSize, orderStatus, payStatus, buyerUserId, orderId } = ctx.request.body;
        const res = await controller_find({
            current, pageSize, orderStatus, payStatus, buyerUserId, orderId
        })
        ctx.body = res
    } catch (error) {
        console.log(error)
        ctx.body = serverError
    }
})



module.exports = router